<?php
require_once("./define/define_table.php");
require_once("./class/Participant.php");

define("ADD_NOTE_FORM", "Formulaire de notation");

class Note {

		private $id_individu;
		private $id_evenement;
		private $note;

    public function Note(){
    }
    
    public function getIdIndividu(){
        return $this->id_individu;
    }
    
    public function getIdEvenement(){
        return $this->id_evenement;
    }
    
    public function getNote(){
        return $this->note;
    }
    
    public function setIdIndividu($id){
        $this->id_individu = $id;
    }
    
    public function setIdEvenement($id){
        $this->id_evenement = $id;
    }
    
    public function setNote($note){
        $this->note= $note;
    }
    
    public function addOrModify(){
        global $db;
				$sql = "SELECT COUNT(*) FROM ".NOTE_TABLE." WHERE id_individu = :id_individu AND id_evenement = :id_evenement";
				$sth = $db->prepare($sql);
				$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
				$sth->bindValue(":id_evenement", $this->id_evenement, PDO::PARAM_INT);
				$sth->execute();
				$nb = $sth->fetch();
				if($nb[0] != 0) {
					die(_ALREADY_VOTED_);
				}
				else {
					$sql = "INSERT INTO ".NOTE_TABLE." VALUES(:id_individu, :id_evenement, :note)";
					$sth = $db->prepare($sql);
					$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
					$sth->bindValue(":id_evenement", $this->id_evenement, PDO::PARAM_INT);
					$sth->bindValue(":note", $this->note);
					$sth->execute();
					PrintQuery::queryPrint($sth);
				}

		}

	public function getAddForm(){
		$html = ADD_NOTE_FORM.
				'<form id=form_note" action="?module=note&action=doAdd" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._ID_EVENEMENT_.'</td>' .
				'			<td><input readonly type="text" name="id_evenement" value = "'.$this->id_evenement.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._ID_INDIVIDU_.'</td>' .
				'			<td>'.Participant::generateSelect($this->id_evenement).'</td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._NOTE_.'</td>' .
				'			<td><input type="text" name="note" onchange="if(this.value < 0 || this.value > 10 || isNaN(this.value)){ alert(\'0 <= NOTE <= 10\'); document.getElementById(\'submit_note_form\').disabled=true;} else{document.getElementById(\'submit_note_form\').disabled=false;} " /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input id="submit_note_form" type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}
	
	public function delete(){
	    global $db;
	    $sql = "DELETE FROM ".NOTE_TABLE." WHERE id_individu = :id_individu AND id_evenement = :id_evenement";
	    $sth = $db->prepare($sql);
	    $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
	    $sth->bindValue(":id_evenement", $this->id_evenement, PDO::PARAM_INT);
	    $sth->execute();
	    PrintQuery::queryPrint($sth);
	}
	
}

?>
